package com.dy.线性表.链表;

public class DulLinkList {
    public DulNode head ;

    public DulLinkList(){
        head = new DulNode();
        head.prior = head ;
        head.next = head;
    }

    public void insert(int i,Object data) throws Exception {
        DulNode p = head.next;
        int j =0;
        while(!p.equals(head) &&j<i){
            p=p.next;
            ++j;
        }
        if(j!=i ) throw new Exception("插入位置不合法");
        DulNode dulNode = new DulNode(data);
        p.prior.next = dulNode;
        dulNode.prior = p.prior;
        dulNode.next = p;
        p.prior = dulNode;

    }

    public void remove(int i ) throws Exception{
        DulNode p = head.next;
        int j =0;
        while(j<i && !p.equals(head)){
            j++;
            p=p.next;
        }
        if(j!=i ) throw new Exception("删除位置不合法");
        p.prior.next = p.next;
        p.next.prior = p.prior;
    }
    public void display(){
        DulNode node = head.next;
        while(!node.equals(head)){
            System.out.println(node.data);
            node=node.next;
        }
        System.out.println();
    }
}
